package com.lwz.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwz.eduservice.entity.EduCourse;
import com.lwz.eduservice.entity.EduTeacher;
import com.lwz.eduservice.entity.vo.CourseInfoVo;
import com.lwz.eduservice.entity.vo.CoursePublishVo;
import com.lwz.eduservice.entity.vo.CourseQuery;
import com.lwz.eduservice.service.EduCourseService;
import com.lwz.util.R;
import com.lwz.util.orderVo.CourseWebVoOrder;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Lw中
 * @since 2020-11-11
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;

    /**
     * LWZ TODO : 2020/11/13 在前后端开发的时候需要将返回参数的属性值和前端的接收值对应上
     */

    /**
     * 课程数据条件查询
     */
    @ApiOperation(value = "条件分页查询")
    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @ApiParam(name = "current", value = "当前页数", required = true)
            @PathVariable long current,

            @ApiParam(name = "limit", value = "每页条数", required = true)
            @PathVariable long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery) {
        //创建page对象
        Page<EduCourse> coursePage = new Page<>(current,limit);

        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        // 多条件组合查询
        // mybatis学过 动态sql
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        //判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)) {
            //构建条件
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq("status",status);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        eduCourseService.page(coursePage,wrapper);
        //总记录数
        long total = coursePage.getTotal();
        //数据list集合
        List<EduCourse> records = coursePage.getRecords();
        return R.ok().data("total",total).data("rows",records);
    }

    /**
     * 课程列表
     * @return
     */
    @GetMapping
    public R getCourseList() {
        List<EduCourse> eduCourseList = eduCourseService.list(null);
        return R.ok().data("list", eduCourseList);
    }

    /**
     * 添加完课程基本信息之后因为要给课程添加课程大纲，所以需要返回课程的id
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        String id = eduCourseService.saveCourse(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    /**
     * 通过课程id获取课程的基本信息
     * @param courseId
     * @return
     */
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        System.out.println("课程ID" + courseId);
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    /**
     * 修改课程信息
     * @param courseInfoVo
     * @return
     */
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    /**
     * 通过课程id获取发布课程所需要的校验信息
     * @param id
     * @return
     */
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo =  eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    /**
     * 通过修改课程的状态完成课程的最终发布
     * @param id
     * @return
     */
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }

    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        eduCourseService.removeCourse(courseId);
        return R.ok();
    }

}

